﻿using System;
using DTcms.Common;
using System.Web.Mvc;

namespace DTcms.Web.Mvc.UI.Page
{
    public partial class article_list : BaseController
    {
        protected int page;         //当前页码
        protected int category_id;  //类别ID
        protected string call_index; //类别别名
        protected string paramString;//分页参数
        protected string redirect = string.Empty;//回传地址

        protected Model.article_category category = new Model.article_category(); //分类的实体

        /// <summary>
        /// OnInit事件,让频道名称变量先赋值
        /// </summary>
        public article_list(Model.site_channel site_model)
        {
            page = DTRequest.GetQueryInt("page", 1);
            call_index = DTRequest.GetQueryString("call_index");
            category_id = DTRequest.GetQueryInt("category_id");
            if (category_id == 0)
            {
                category_id = DTRequest.GetQueryInt("id");
            }
            #region 验证频道列表数据
            BLL.article_category bll = new BLL.article_category();
            category.title = "所有类别";
            if (category_id > 0)
            {
                category = bll.GetModel(site_model.id, category_id);
                if (category == null)
                {
                    redirect = linkurl("error", "?msg=" + Utils.UrlEncode("类别验证错误！"));
                    return;
                }
            }
            else if (!string.IsNullOrEmpty(call_index)) //否则检查设置的别名
            {
                category = bll.GetModel(site_model.id, call_index);
                if (category == null)
                {
                    redirect = linkurl("error", "?msg=" + Utils.UrlEncode("类别验证错误！"));
                    return;
                }
                category_id = category.id;
            }
            //判断是否是链接
            if (!string.IsNullOrEmpty(category.link_url))
            {
                redirect = category.link_url;
                return;
            }
            //判断SEO标题
            if (string.IsNullOrEmpty(category.seo_title))
            {
                category.seo_title = site.seo_title;
            }
            if (string.IsNullOrEmpty(category.seo_keywords))
            {
                category.seo_keywords = site.seo_keywords;
            }
            if (string.IsNullOrEmpty(category.seo_description))
            {
                category.seo_description = site.seo_description;
            }
            //分页参数
            if (!string.IsNullOrEmpty(category.call_index))
            {
                paramString = category.call_index;
            }
            else if (category_id > 0)
            {
                paramString = category_id.ToString();
            }
            #endregion

            if (category_id > 0) //如果ID获取到，将使用ID
            {
                if (bll.Exists(category_id))
                {
                    category = bll.GetModel(category_id);
                }
            }
        }
    }
}
